package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panshi.modules.domain.Carousel;
import com.panshi.modules.mapper.CarouselMapper;
import com.panshi.modules.service.CarouselService;
import com.panshi.vo.CarouselVO;
import com.panshi.vo.StatusVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper mapper;

    @Override
    public List<CarouselVO> listCarousel(StatusVO statusVO) {
        List<CarouselVO> carouselVOS = new ArrayList<>();

        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();

        if(statusVO != null && statusVO.getStatus() != null){
            wrapper.eq("status",statusVO.getStatus());
        }
        List<Carousel> list = mapper.selectList(wrapper);
        if(list.size() > 0){
            for (Carousel carousel :list){
                CarouselVO carouselVO = new CarouselVO();
                BeanUtils.copyProperties(carousel,carouselVO);
                carouselVOS.add(carouselVO);
            }
        }
        return carouselVOS;
    }

    @Override
    public int editCarousel(CarouselVO carouselVO) {
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselVO,carousel);
        int edit = mapper.updateById(carousel);
        return edit;
    }

    @Override
    public int addCarousel(CarouselVO carouselVO) {
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselVO,carousel);
        int insert = mapper.insert(carousel);
        return insert;
    }
}
